﻿@page "/role"
@using System.Collections.ObjectModel
@inject DialogService DialogService
@inject NotificationService NotificationService
@inject IRBACRoleService RBACRoleService

<RadzenText TextStyle="TextStyle.H4">角色管理</RadzenText>
<PageTitle>角色管理</PageTitle>
<RadzenStack Style="display: flex; margin: 0px 10px 10px 0px" Orientation="Orientation.Horizontal" JustifyContent="JustifyContent.Start" AlignItems="AlignItems.Center">
    <RadzenTextBox @bind-Value="@keyword" Placeholder="角色名称" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="查询" Icon="refresh" Click=@(args => Search()) ButtonStyle="ButtonStyle.Primary" Style="margin:10px 0px 10px 0px" />
    <RadzenButton Text="新增" Icon="add_circle" Click=@(args => Add()) ButtonStyle="ButtonStyle.Primary" Style="margin:0px" />
</RadzenStack>
<RadzenDataGrid EmptyText="暂无数据" TItem="RoleDto" Data="@roles" GridLines="Radzen.DataGridGridLines.Default" AllowPaging="true" AllowSorting="true">
    <Columns>
        <RadzenDataGridColumn Property="" Title="操作" Width="290px">
            <Template Context="data">
                <RadzenStack Gap="10" Style="margin-bottom: 10px" Orientation="Orientation.Horizontal">
                    <RadzenButton Icon="edit" @onclick:stopPropagation Click="@(args=> Update(data))" ButtonStyle="ButtonStyle.Primary" />
                    <RadzenButton Icon="delete" @onclick:stopPropagation Click="@(args=> Delete(data))" ButtonStyle="ButtonStyle.Danger" />
                    <RadzenButton Icon="report" @onclick:stopPropagation Click="@(args=> Grant(data))" ButtonStyle="ButtonStyle.Success" />
                </RadzenStack>
            </Template>
        </RadzenDataGridColumn>
        <RadzenDataGridColumn Property="Name" Title="角色名称" />
        <RadzenDataGridColumn Property="Remark" Title="说明" />
    </Columns>
    <LoadingTemplate>
        <RadzenProgressBarCircular ProgressBarStyle="ProgressBarStyle.Primary" Value="100" ShowValue="false" Mode="ProgressBarMode.Indeterminate" />
    </LoadingTemplate>
</RadzenDataGrid>

@code {
    string keyword = default!;
    ObservableCollection<RoleDto> roles = default!;

    private async Task Search()
    {
        var list = await RBACRoleService.GetListAsync();
        roles = new ObservableCollection<RoleDto>(list);    
    }

    private async void Add()
    {
        var result = await DialogService.OpenAsync<RoleAddDialog>(
            $"新增角色",
            new Dictionary<string, object>(){{"PRole", new RoleDto()}},
            new DefaultDialogOptions());

        if(result != null)
        {
            roles!.Add(result);
        }
    }

    private async void Update(RoleDto selectedItem)
    {
        RoleDto result = await DialogService.OpenAsync<RoleAddDialog>(
            $"修改角色",
            new Dictionary<string, object>() {{ "PRole", selectedItem }},
            new DefaultDialogOptions());
                      
        if(result != null)
        {
            selectedItem.Name = result.Name;
            selectedItem.Remark = result.Remark;
            StateHasChanged();            
        }
    }

    private async Task Delete(RoleDto selectedRole)
    {
        bool? result = await DialogService.Confirm("确认要删除吗?", "提示", new ConfirmOptions() 
        { 
            OkButtonText = "确定", 
            CancelButtonText = "取消"
        });

        if ((bool)result!)
        {
            try
            {
                await RBACRoleService.Delete(selectedRole.Id);
                roles!.Remove(selectedRole);
            }
            catch (Exception ex)
            {
                NotificationService.Notify(new NotificationMessage { Severity = NotificationSeverity.Error, Summary = "错误", Detail = ex.Message });
            }
        }
    }
    
    private async Task Grant(RoleDto selectedRole)
    {
        await DialogService.OpenAsync<RoleGrantRightDialog>($"分配权限",
            new Dictionary<string, object>() { { "PRoleId", selectedRole.Id } },
            new DialogOptions()
                {
                    Resizable = true,
                    Draggable = true,
                    Width = "700px",
                    Height = "512px",
                    Left = null,
                    Top = null
                });
    }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        await Search();
    }

}
